package com.gouldzd.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.gouldzd.constant.MessageConstant;
import com.gouldzd.constant.RedisConstant;
import com.gouldzd.entity.Result;
import com.gouldzd.pojo.OrderSetting;
import com.gouldzd.service.OrderSettingService;
import com.gouldzd.service.SetMealService;
import com.gouldzd.utils.AliyunUtils;
import com.gouldzd.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/*
 * @Author zhangdan
 * @Description //TODO 文件上传管理
 * @Date 19:19 2020/4/30
 * @Param
 * @return
 **/
@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetMealService setMealService;

    @Reference
    private OrderSettingService orderSettingService;

    //套餐图片文件上传到阿里云
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        System.out.println(imgFile);
        //获取原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        //获取最后一个 . 的索引
        int lastIndex = originalFilename.lastIndexOf(".");//.jpg
        //截取包含 . 的后缀
        String extention = originalFilename.substring(lastIndex);
        //拼接文件名,使用UUID随机取名.防止文件名重复
        String fileName = UUID.randomUUID().toString()+extention;

        try {
            //将文件上传到阿里云服务器
            AliyunUtils.upload2Aliyun(imgFile.getBytes(),fileName);
            //将文件名存到redis集合中
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }

        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
    }


    //文件上传,实现预约设置数据批量导入
    @RequestMapping("/uploadExcel")
    public Result uploadExcel(@RequestParam("excelFile") MultipartFile excelFile){

        try {
            //使用POI解析表格数据
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<OrderSetting> data = new ArrayList<>();
            //遍历list
            for (String[] strings : list) {
                //第一列:预约日期
                String orderDate = strings[0];
                //第二列:预约人数
                String number = strings[1];
                //将数据封装到OrderSetting并存入list集合中
                OrderSetting orderSetting = new OrderSetting(new Date(orderDate), Integer.parseInt(number));
                data.add(orderSetting);
            }
            //通过dubbo远程调用服务实现数据批量导入到数据库
            orderSettingService.add(data);
            return  new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            //文件解析失败
            return  new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }
}
